<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

/**
 * 后台内容控制器
 * 
 * @author huajie <banhuajie@163.com>
 */
class LearningCenterController extends AdminController
{

    public function index() {
              
        $list=$this->lists('learning_center');
        $this->assign('_list', $list);
        $this->meta_title = "学院";
        $this->display();
    }
    
    public function add($type = 'doc'){
        $M_learning_center = M('learning_center');        
       
            $this->meta_title = '新增';
            if($type=='doc'){
                $this->display('docedit');
            }else{
                $this->display('videoedit');
            }
    }

    public function edit(){
        if (IS_POST) {
            $data = params_request(array('title', 'type', 'sort', 'description',
                    'resources_link','head_pic','link','movie'));
        
            !preg_match('/^.{2,50}$/', $data['title']) && $this->error('标题长度必须在4~60个字符');
            $data['uid'] = UID;
            $data['update_time'] = NOW_TIME;
            $M_learning_center=M('learning_center');
            if(I('movie')){
               if(I('thumb_cover_id')){                 
                     M('movie')->where(array('id'=>I('movie')))->save(array('thumb_cover_id'=>I('thumb_cover_id')));
                }
            }
            $id=I('id',0);
            if($id){              
                $id = $M_learning_center->where(array('id'=>$id))->save($data);
            }else{
                $data['create_time'] = NOW_TIME;               
                $id = $M_learning_center->add($data);
            }        
            $id > 0 ? $this->success('保存成功', U('index')) : $this->error('保存失败，未知错误');
        }else{
            $id = I('id');            
            $M_l_c = M('learning_center');
            
            $info = $M_l_c->find($id);
            !$info && $this->error('非法参数');
            $this->assign('info',$info);
            
            if($info['movie']){
                $thumb_cover_id=get_movie($info['movie'],'thumb_cover_id');
                $this->assign('thumb_cover_id',$thumb_cover_id);
            }
            if($info['type']=='doc'){
                $this->display('docedit');
            }else{
                $this->display('videoedit');
            }
        }
    }

    public function delete() {
        $ids = I('id');
        
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        
        $M_article = M('Help');
        
        $map    =   array();
        
        if(is_array($ids)){
            $map['id|pid'] = array('in', $ids);
        }elseif (is_numeric($ids)){
            $map['id|pid'] = $ids;
        }
        
        $M_article->where($map)->delete()!==false ? $this->success('删除成功！','',IS_AJAX) : $this->error('删除失败！','',IS_AJAX);
        
    }
}
